package Service;

import Dao.Impl.RentalRecordDAOImpl;
import pojo.ExcelExporter;
import pojo.RentalRecord;

import java.io.*;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Stream;

public class CarRentalService {
    private RentalRecordDAOImpl rentalRecordDAOImpl;

    public CarRentalService() throws Exception {
        rentalRecordDAOImpl = new RentalRecordDAOImpl();
    }

    //获取所有订单信息（管理员界面）
    public List<RentalRecord> getAllRental() throws Exception {
        return rentalRecordDAOImpl.getAllRental();
    }

    //管理员界面，更新订单状态“取消”
    public void updatestatus(int quit_rentalid) throws Exception {
        rentalRecordDAOImpl.updatestatus(quit_rentalid);
    }

    //根据车辆id查询数据，管理员界面
    public List<RentalRecord> getRental_ByCarid(int searchText) throws Exception {
        return rentalRecordDAOImpl.getRental_ByCarid(searchText);
    }

    //管理员界面进行订单导出
    public void getAllRentalIO() throws Exception {
        List<RentalRecord> recordList = rentalRecordDAOImpl.getAllRental();

        String orderFileName = "订单文件";

        //excel表的导出
        ExcelExporter<RentalRecord> excelExporter = new ExcelExporter<>(recordList, "F:\\桌面2\\project\\"+orderFileName+".xlsx", "车辆租赁订单报表");
        excelExporter.exportToExcel();

        //进行文件操作
        try(FileOutputStream fileOutputStream = new FileOutputStream("F:\\桌面2\\project\\" + orderFileName + ".txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
                ){
            for (RentalRecord record:recordList){
                objectOutputStream.writeObject(record);
            }
            objectOutputStream.writeObject(null);

        }catch (Exception e){
            e.printStackTrace();
        }

        try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("F:\\桌面2\\project\\" + orderFileName + ".txt"))){
            Object o=null;
            while ((o = objectInputStream.readObject())!=null){
              RentalRecord record = (RentalRecord) o;
              System.out.println(record);
          }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
